package com.bank.backend.controller;

import com.bank.backend.model.entity.User;
import com.bank.backend.model.vo.UserVO;
import com.bank.backend.result.Result;
import com.bank.backend.service.UserService;
import com.bank.backend.model.dto.CustomerUpdateDTO;
import com.bank.backend.service.DepositService;
import com.bank.backend.model.dto.WithdrawDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.bank.backend.model.dto.CardQueryDTO;
import com.bank.backend.model.vo.CardVO;
import com.bank.backend.result.PageResult;
import com.bank.backend.service.CardManageService;
import com.bank.backend.model.dto.TradeQueryDTO;
import com.bank.backend.model.vo.TradeVO;
import com.bank.backend.service.TradeService;
import com.bank.backend.model.dto.CardPasswordUpdateDTO;
import com.bank.backend.model.dto.UserPasswordUpdateDTO;
import com.bank.backend.model.dto.TransferDTO;
import com.bank.backend.utils.UserHolder;

import javax.annotation.Resource;

/**
 * 客户相关接口
 */
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Resource
    private UserService userService;

    @Resource
    private DepositService depositService;

    @Resource
    private CardManageService cardManageService;

    @Resource
    private TradeService tradeService;

    /**
     * 获取当前客户信息
     * @return 当前客户信息
     */
    @GetMapping("/me")
    public Result<UserVO> getCurrentCustomerInfo() {
        User user = userService.getUser();
        if (user == null) {
            return Result.error("未登录或登录已过期");
        }
        UserVO userVO = userService.getUserVO(user);
        return Result.success(userVO);
    }

    /**
     * 编辑当前客户信息
     * @param updateDTO 客户信息
     * @return 最新客户信息
     */
    @PutMapping("/me")
    public Result<UserVO> updateCustomer(@RequestBody CustomerUpdateDTO updateDTO) {
        UserVO userVO = userService.updateCustomer(updateDTO);
        return Result.success(userVO);
    }

    /**
     * 存款操作
     * @param cardId 卡号
     * @param amount 存款金额
     * @param remark 备注
     * @return 操作成功
     */
    @PostMapping("/cards/{card_id}/deposit")
    public Result deposit(@PathVariable("card_id") String cardId, @RequestParam("amount") Double amount, @RequestParam(value = "remark", required = false) String remark) {
        depositService.deposit(cardId, amount, remark);
        return Result.success(null, "操作成功");
    }

    /**
     * 取款操作
     * @param dto 取款参数（含卡号）
     * @return 操作成功
     */
    @PostMapping("/cards/withdraw")
    public Result withdraw(@RequestBody WithdrawDTO dto) {
        depositService.withdraw(dto);
        return Result.success(null, "操作成功");
    }

    /**
     * 分页查询客户自己的所有银行卡信息（可多条件筛选）
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/cards/list")
    public Result<PageResult<CardVO>> listCards(CardQueryDTO queryDTO) {
        Integer currentUserId = UserHolder.getUserId();
        queryDTO.setUserId(currentUserId);
        PageResult<CardVO> pageResult = cardManageService.pageList(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 分页查询银行卡交易记录（多条件筛选、分页、卡号可选）
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/cards/transactions")
    public Result<PageResult<TradeVO>> listTransactions(TradeQueryDTO queryDTO) {
        PageResult<TradeVO> pageResult = tradeService.pageList(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 客户挂失/解挂银行卡
     * @param cardId 卡号
     * @return 操作结果
     */
    @PostMapping("/cards/{card_id}/report-loss")
    public Result cardReportLoss(@PathVariable("card_id") String cardId) {
        Integer userId = UserHolder.getUserId();
        CardVO card = cardManageService.getCardByCardId(cardId);
        if (card == null) {
            return Result.error("未找到该银行卡");
        }
        if (!userId.equals(card.getUserId())) {
            return Result.error("无权限操作该银行卡");
        }
        Integer currentStatus = card.getIsReportLoss();
        Integer newStatus = (currentStatus != null && currentStatus == 1) ? 0 : 1;
        cardManageService.updateReportLossStatus(cardId, newStatus);
        return Result.success(null, newStatus == 1 ? "挂失成功" : "解挂成功");
    }

    /**
     * 修改银行卡密码
     * @param passwordUpdateDTO 密码修改DTO
     * @return 操作结果
     */
    @PutMapping("/cards/password")
    public Result updateCardPassword(@RequestBody CardPasswordUpdateDTO passwordUpdateDTO) {

        cardManageService.updateCardPassword(passwordUpdateDTO);
        return Result.success(null, "密码修改成功");
    }

    /**
     * 修改用户账户密码
     * @param passwordUpdateDTO 密码修改DTO
     * @return 操作结果
     */
    @PutMapping("/password")
    public Result updateUserPassword(@RequestBody UserPasswordUpdateDTO passwordUpdateDTO) {
        userService.updateUserPassword(passwordUpdateDTO);
        return Result.success(null, "账户密码修改成功");
    }

    /**
     * 银行卡转账
     * @param transferDTO 转账DTO
     * @return 操作结果
     */
    @PostMapping("/transfer")
    public Result transfer(@RequestBody TransferDTO transferDTO) {
        tradeService.transfer(transferDTO);
        return Result.success(null, "转账成功");
    }
} 